# 导入Tortoise ORM的Model基类
from tortoise.models import Model
# 导入Tortoise ORM的字段类型
from tortoise import fields


class User(Model):
    """
    用户模型
    """
    id = fields.IntField(pk=True, description="用户ID")
    name = fields.CharField(max_length=255, description="用户名")
    pwd = fields.CharField(max_length=255, description="密码")
    role_id = fields.IntField(default=1, description="用户角色ID，默认1为普通用户")
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")
    
    class Meta:
        table = "users"
        table_description = "用户表"


class DingtalkUser(Model):
    """
    钉钉用户模型
    """
    id = fields.IntField(pk=True, description="主键ID")
    unionid = fields.CharField(max_length=50, unique=True, description="钉钉用户唯一标识")
    openid = fields.CharField(max_length=50, unique=True, description="钉钉用户开放ID")
    nickname = fields.CharField(max_length=100, null=True, description="用户昵称")
    avatar_url = fields.TextField(null=True, description="用户头像URL")
    access_token = fields.TextField(description="访问令牌")
    refresh_token = fields.TextField(description="刷新令牌")
    expire_at = fields.DatetimeField(description="访问令牌过期时间")
    user = fields.ForeignKeyField('models.User', related_name='dingtalk_accounts', null=True, description="关联的本地用户")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    
    class Meta:
        table = "dingtalk_users"
        table_description = "钉钉用户表"


class RealNameAuth(Model):
    """
    实名信息表
    """
    id = fields.IntField(pk=True, description="主键ID")
    user_id = fields.IntField(description="用户ID")
    image_url = fields.TextField(description="证件图片地址")
    name = fields.CharField(max_length=100, description="姓名")
    id_number = fields.CharField(max_length=32, description="证件号")
    address = fields.TextField(description="地址")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")

    class Meta:
        table = "real_name_auth"
        table_description = "实名认证信息表"